// 额外引入了一个RouteRecordRaw类型
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Reg from "../views/Reg.vue";
import Login from "../views/Login.vue";
import Guider from "../views/Guider.vue";
import Del from "../views/Del.vue";

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/reg',
        name: 'reg',
        component: Reg
    }, {
        path: '/guider',
        name: 'guider',
        component: Guider
    },
    {
        path: '/del',
        name: 'del',
        component: Del
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((from, to, next) => {
    //当前无论是到达/home，还是从home离开，都要被路由守卫拦截，并判断是否登录过，从而决定是 否跳转到guider路由(也就是引导页)
    if (to.path === '/login' || from.path === '/login') {
        //获取本地存储中记录是否登录过的属性isVisited
        let isVisited = localStorage.getItem('isVisited') || ''
        if (isVisited) {
            // 如果登录过，  正常该去哪去哪
            return next()
        } else {
            // 如果没有登录过，设置登录过的信息存到本地存储
            localStorage.setItem('isVisited', 'aaa')
            //重定向去引导页
            return next('/guider')
        }
    }
    next()
})

export default router;